import numeral from 'numeral';
import _find from 'lodash/find';
import moment from 'moment/src/moment.js';
import locations from 'assets/locations.js';
import 'toastr/build/toastr.css';
import toastr from 'toastr/toastr.js';

// 对象深度克隆
export function DeepClone(obj) {
    if (!obj) return null;
    return JSON.parse(JSON.stringify(obj));
}

// 显示成功提示
export function ShowSuccess(title, message) {
    toastr.options = {
        "closeButton": true,
        "debug": false,
        "positionClass": "toast-top-right",
        "onclick": null,
        "showDuration": "1000",
        "hideDuration": "1000",
        "timeOut": "5000",
        "extendedTimeOut": "1000",
        "showEasing": "swing",
        "hideEasing": "linear",
        "showMethod": "fadeIn",
        "hideMethod": "fadeOut"
    };
    toastr.success(message, title);
}

// 定义货币格式化过滤器
export function CurrencyFormat(value) {
    return numeral(value).format('0,0.00');
}

// 支付方式
export function PayMethodFormat(value) {
    switch (value) {
        case 'BANK':
            return '银行转账';
        case 'BANK2':
            return '国外银行转账';
        case 'ALIPAY':
            return '支付宝支付';
        case 'WEIXIN':
            return '微信支付';
        case 'PAYPAL':
            return 'PayPal';
    }
}

// 支付项类型
export function PriceTypeFormat(value) {
    switch (value) {
        case 'APHF|PASSPORT':
            return '通行证';
        case 'APHF|COMPETE|SOLO':
            return '独奏比赛';
        case 'APHF|COMPETE|ENSEMBLE':
            return '重奏/合奏比赛';
        case 'APHF|CONCERT':
            return '音乐会';
    }
}

// 格式化国家
export function CountryFormat(user) {
    let _country = _find(locations, { Code: user.Country });
    if (_country == null) return null;

    return _country['Name-zh-CN'] + ' ' + _country['Name-en'].toUpperCase();
 }

// 格式化国家/地区
export function LocationFormat(country, state) {
    let _country = _find(locations, { Code: country });
    if (_country == null) return null;

    let _state = _find(_country.State, { Code: state });
    return {
        country: _country['Name-zh-CN'],
        state: _state ? _state['Name-zh-CN'] : ''
    };
}

// 格式化国家/地区
export function LocationStringFormat(country, state) {
    let ret = LocationFormat(country, state);
    if (ret == null) return null;
    return ret.country == ret.state ? ret.country : ret.country + ' ' + ret.state;
}

// 格式化国家/地区
export function LocationStringFormatFromUser(user) {
    return LocationStringFormat(user.Country, user.State);
}

//格式化日期
Date.prototype.Format = function(fmt) {
    var o = {
        "y+": this.getFullYear(),
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "h+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S+": this.getMilliseconds() //毫秒
    };
    for (var k in o) {
        if (new RegExp("(" + k + ")").test(fmt)) {
            if (k == "y+") {
                fmt = fmt.replace(RegExp.$1, ("" + o[k]).substr(4 - RegExp.$1.length));
            } else if (k == "S+") {
                var lens = RegExp.$1.length;
                lens = lens == 1 ? 3 : lens;
                fmt = fmt.replace(RegExp.$1, ("00" + o[k]).substr(("" + o[k]).length - 1, lens));
            } else {
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
            }
        }
    }
    return fmt;
}

Date.prototype.addMinutes = function(minutes) {
    let dt = this.setMinutes(this.getMinutes() + minutes);
    return new Date(dt);
}

Date.prototype.subMinutes = function(minutes) {
    let dt = this.setMinutes(this.getMinutes() - minutes);
    return new Date(dt);
}

Date.prototype.addDays = function(days) {
    let dt = this.setDate(this.getDate() + days);
    return new Date(dt);
}

Date.prototype.subDays = function(days) {
    let dt = this.setDate(this.getDate() - days);
    return new Date(dt);
}

const WEEK_NAME = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
Date.prototype.getWeekday = function() {
    return WEEK_NAME[this.getDay()];
}

Date.prototype.Date = function() {
    return new Date(this.Format('yyyy-MM-dd'));
}

// 用于格式化时间字符串
// YYYY-mm-dd HH:mm:ss
export function TimeFormat(time) {
    if (!time) return null;
    return new Date(time).Format('yyyy-MM-dd hh:mm');
}

export function TimeFormat1(time) {
    if (!time) return null;
    return new Date(time).Format('hh:mm');
}

// 用于格式化出生日期
// YYYY-mm-dd
export function BirthFormat(time) {
    return new Date(time).Format('yyyy-MM-dd');
}

// 用于将UTC时间转换为本地时间+8小时
export function LocaltimeFormat(time) {
    return moment(time).add(8, 'hour').format('YYYY-MM-DD HH:mm:ss');
}

// 定义各类事件对象
export const DAYS = {
    Today: moment().startOf('day').format('YYYY-MM-DD HH:mm:ss'),
    Tomorrow: moment().startOf('day').add(1, 'day').format('YYYY-MM-DD HH:mm:ss')
}

// 获取日期控件的快捷选择按钮
export function GetPickerShortcuts() {
    let formatStr = 'YYYY-MM-DD';
    return {
        shortcuts: [{
            text: '今日',
            onClick(picker) {
                const start = moment().startOf('day').format(formatStr);
                const end = moment().startOf('day').add(1, 'day').format(formatStr);
                picker.$emit('pick', [start, end]);
            }
        }, {
            text: '昨日',
            onClick(picker) {
                const start = moment().startOf('day').subtract(1, 'day').format(formatStr);
                const end = moment().startOf('day').format(formatStr);
                picker.$emit('pick', [start, end]);
            }
        }, {
            text: '上周',
            onClick(picker) {
                const end = moment().startOf('week').format(formatStr);
                const start = moment(end).subtract(7, 'day').format(formatStr);
                picker.$emit('pick', [start, end]);
            }
        }, {
            text: '本周',
            onClick(picker) {
                const start = moment().startOf('week').format(formatStr);
                const end = moment().startOf('week').add(7, 'day').format(formatStr);
                picker.$emit('pick', [start, end]);
            }
        }, {
            text: '上月',
            onClick(picker) {
                const end = moment().startOf('month').format(formatStr);
                const start = moment(end).subtract(1, 'day').startOf('month').format(formatStr);
                picker.$emit('pick', [start, end]);
            }
        }, {
            text: '本月',
            onClick(picker) {
                const start = moment().startOf('month').format(formatStr);
                const end = moment().endOf('month').add(1, 'day').format(formatStr);
                picker.$emit('pick', [start, end]);
            }
        }, {
            text: '今年',
            onClick(picker) {
                const start = moment().startOf('year').format(formatStr);
                const end = moment().endOf('year').format(formatStr);
                picker.$emit('pick', [start, end]);
            }
        }]
    }
}

// XML格式化
export function FormatXml(xml) {
    var formatted = '';
    var reg = /(>)(<)(\/*)/g;
    xml = xml.replace(reg, '$1\r\n$2$3');
    var pad = 0;
    jQuery.each(xml.split('\r\n'), function(index, node) {
        var indent = 0;
        if (node.match( /.+<\/\w[^>]*>$/ )) {
            indent = 0;
        } else if (node.match( /^<\/\w/ )) {
            if (pad != 0) {
                pad -= 1;
            }
        } else if (node.match( /^<\w[^>]*[^\/]>.*$/ )) {
            indent = 1;
        } else {
            indent = 0;
        }

        var padding = '';
        for (var i = 0; i < pad; i++) {
            padding += '  ';
        }

        formatted += padding + node + '\r\n';
        pad += indent;
    });

    return formatted;
}
